//
//  DBJNewFeatureView.m
//  DBJ
//
//  Created by 花化华 on 2016/11/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "DBJNewFeatureView.h"
#import <Masonry.h>

@interface DBJNewFeatureView () <UIScrollViewDelegate>

@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIPageControl *pageControl;

@end

@implementation DBJNewFeatureView

- (void)dbjSetupSubviews{
    [super dbjSetupSubviews];
}

#pragma mark - 重写imgs属性的set方法
- (void)setImgs:(NSArray *)imgs{
    
    //传值
    _imgs = imgs;
    
    //创建scrollView
    [self addScrollView];
    
    //创建页面指示器
    [self addPageControl];
    
}

#pragma mark - scrollView相关
/**
 *  /创建scrollView
 */
- (void)addScrollView{
    //提取当前view的宽度作为每个图片元素的宽度
    CGFloat width = self.bounds.size.width;
    
    //1.创建scrollView
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
    self.scrollView = scrollView;
    //--为scrollView设置代理
    scrollView.delegate =self;
    
    //2.循环为scrollView设置内容
    for (int i = 0; i < _imgs.count; i++) {
        //--获取当前图片
        UIImage *img = _imgs[i];
        //--创建相框
        UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
        //--设置相框大小和坐标（偏移值）
        imgView.frame = CGRectOffset(self.bounds, i*width, 0);
        //--设置相框为可编辑状态
        imgView.userInteractionEnabled = YES;
        //--添加到scrollView
        [scrollView addSubview:imgView];
        //添加约束
        //        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.edges.equalTo(scrollView).mas_offset(UIEdgeInsetsMake(0, 0, 0, 0));
        //        }];
    }
    
    //3.设置scrollView的contentSize
    scrollView.contentSize = CGSizeMake((_imgs.count+1)*width, 0);
    //4.设置scrollView的分页效果
    scrollView.pagingEnabled = YES;
    //5.禁用scrollView的弹簧效果
    scrollView.bounces = NO;
    //6.禁用scrollView的垂直和水平的view
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    
    //7.将scrollView添加到父容器中
    [self addSubview:scrollView];
    
    //8.为scrollView的每个imgView中添加按钮
    [self addImgViewBtn];
    
    //9.为scrollView添加约束
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).mas_offset(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

/**
 *  为scrollView的每个imgView中添加按钮
 */
- (void)addImgViewBtn{
    
    for (int i = 0; i < _imgs.count ; i++) {
        if (i == 3) {
            UIImageView *imgView = self.scrollView.subviews[3];
            
            // 跳过按钮
            UIButton *btn = [[UIButton alloc] init];
            [btn setTitle:@" Skip " forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor whiteColor];
            btn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
            [btn setTitleColor:DBJColor forState:UIControlStateNormal];
            btn.layer.cornerRadius = 13;
            
            //添加到imgView中
            [imgView addSubview:btn];
            
            //添加约束
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(80, 40));
                make.right.equalTo(imgView).offset(-29);
                make.bottom.equalTo(imgView).offset(-80);
            }];
            
            //为按钮添加点击事件
            [btn addTarget:self action:@selector(btnClear:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
}

/**
 *  imgView中的按钮的点击事件
 */
- (void)btnClear:(UIButton *)sender{
    
    //应该放大被点击按钮所在的图片框 sender.superview;就可以获取到被点击按钮所在的图片框
    //sender就是谁点击就是谁
    //获取按钮所属的当前imgView
    UIView *imgView = sender.superview;
    
    //动画
    [UIView animateWithDuration:1.9 animations:^{
        
        //放大倍数
        int scale = 3.0;
        //放大
        imgView.transform = CGAffineTransformScale(imgView.transform, scale, scale);
        //透明
        imgView.alpha = 0;
        
    } completion:^(BOOL finished) {
        //回收当前开场view
        [self removeFromSuperview];
    }];
    
}

#pragma mark - pageControl相关
/**
 *  创建页面指示器
 */
- (void)addPageControl{
    
    //1.创建pageControl
    UIPageControl *pageControl = [UIPageControl new];
    self.pageControl = pageControl;
    
    //2.为pageControl设置page总个数
    pageControl.numberOfPages = _imgs.count;
    
    //3.为pageControl设置当前page下标
    pageControl.currentPage = 0;
    
//    pageControl.pageIndicatorTintColor = [UIColor redColor];
//    pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    
    //4.添加到父容器中
    [self addSubview:pageControl];
    
    //5.设置不可编辑
    pageControl.userInteractionEnabled = NO;
    
    //6.添加约束
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-80);
    }];
    
}

#pragma mark - scrollView的代理方法
/**
 *  正在滚动
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //当中page
    int page;
    
    //判断是否是最后一页
    //如果是最后一页则控制拉出一点(<100)就隐藏pageControl
//    if (scrollView.contentOffset.x > (scrollView.contentSize.width-2*DBJScreenWidth+100)) {
////        NSLog(@"最后一页");
//        //计算当前偏移值对应的页数（1.0为分解）
//        page = (scrollView.contentOffset.x / self.bounds.size.width) + 1.0;
//    }else{
        //计算当前偏移值对应的页数（0.5为分解）
        page = (scrollView.contentOffset.x / self.bounds.size.width) + 0.5;
//    }
    
    //为指示器更新状态
    self.pageControl.currentPage = page;
    
    //设置指示器是否需要隐藏
    self.pageControl.hidden = page == self.pageControl.numberOfPages;

}

/**
 *  滚动结束
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //如果滑动到最后一页之后时
    if (self.pageControl.hidden) {
        //回收当前开场view
        [self removeFromSuperview];
    }
    
}

@end
